package com.aisino.api.filter;

import jakarta.servlet.*;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.stereotype.Component;

import java.io.IOException;

//@Component
public class RequestWrapperFilter implements Filter {

    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
            throws IOException, ServletException {
        HttpServletRequest httpRequest = (HttpServletRequest) request;
        
        // 对API路径的请求进行包装，以便可以多次读取请求体
        if (httpRequest.getRequestURI().startsWith("/ygydapi/v1/")) {
            // 确保请求未被包装过
            if (httpRequest instanceof RequestWrapper) {
                chain.doFilter(request, response);
            } else {
                RequestWrapper requestWrapper = new RequestWrapper(httpRequest);
                chain.doFilter(requestWrapper, response);
            }
        } else {
            chain.doFilter(request, response);
        }
    }
}